package cn.biplam.back.operator.systemManage.sysprivi.user.table;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.SysManageCode.UserOptCode;
import cn.biplam.common.struc.SysStruc.UserStruc;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 *  用户表数据操作类
 * @author XiaoRuxing Nov 2, 20095:16:34 PM
 * @version 1.0
 */
public class UserOpt extends DataOperator{

	/** 映射信息表名称*/
	public static final String User_Table_Name="USERS";

	static final String Op_ID=UserOptCode.Op_ID;
	
	public UserOpt() throws ErrorMessage {
		super(User_Table_Name);
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(UserOptCode.Query_User_By_Id.equals(opId)){
			this.QueryUserById(sql, filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(UserOptCode.Edit_User.equals(opId)){
			this.editBaseInfo(sql, filter);
			return this.doing(sql,Operator_Type.DML);
		}else if(UserOptCode.Insert_User.equals(opId)){
			this.addBaseInfo(sql, filter);
			return this.doing(sql,Operator_Type.DML);
		}else if(UserOptCode.Del_USer.equals(opId)){
			this.delUser(sql, filter);
			return this.doing(sql,Operator_Type.DML);
		}else if(UserOptCode.Query_By_User_Code.equals(opId)){
			this.QueryUserByCode(sql, filter);
			return this.doing(sql,Operator_Type.Select);
		}else{
			throw new ErrorMessage("Operator Exchange outport not found:"+ opId+" !");
		}
	}
	
	/**
	 *  根据用户编码查询用户信息
	 */
	private void QueryUserByCode(ArrayList<String> sql, DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(UserStruc.USERCODE);
		sp.append("='");
		sp.append(filter.getValue(UserStruc.USERCODE));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 *  根据用户ID查询用户信息
	 */
	private void QueryUserById(ArrayList<String> sql, DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(UserStruc.USERID);
		sp.append("='");
		sp.append(filter.getValue(UserStruc.USERID));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 *  更新用户基本信息
	 */
	private void editBaseInfo(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		StringBuilder sp = new StringBuilder();
		Update update = new Update(this.tableName);
		update.putStringFieldIgnoreNull(UserStruc.USERNAME, filter.getValue(UserStruc.USERNAME));
		update.putStringFieldIgnoreNull(UserStruc.PASSWORD, filter.getValue(UserStruc.PASSWORD));
		//日期类型输入以后加上
//		update.putStringFieldIgnoreNull(UserStruc.BIRTHEDAY, filter.getValue(UserStruc.BIRTHEDAY));
		update.putStringFieldIgnoreNull(UserStruc.LANG, filter.getValue(UserStruc.LANG));
		update.putStringFieldIgnoreNull(UserStruc.SEX, filter.getValue(UserStruc.SEX));
		update.putStringFieldIgnoreNull(UserStruc.USERCODE, filter.getValue(UserStruc.USERCODE));
		update.putStringFieldIgnoreNull(UserStruc.USER_MACHINE, filter.getValue(UserStruc.USER_MACHINE));
		//更新用户
		sp.append(UserStruc.USERID);
		sp.append("='");
		sp.append(filter.getValue(UserStruc.USERID));
		sp.append("'");
		update.setWhere(sp.toString());
		sql.add(update.getSimpleSql());
	}
	
	/**
	 *  添加用户信息
	 */
	private void addBaseInfo(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		Insert insert = new Insert(this.tableName);
		String userId = this.generatorRowID();
		insert.putStringFieldDenyNull(UserStruc.USERID, userId);
		insert.putStringFieldIgnoreNull(UserStruc.USERNAME, filter.getValue(UserStruc.USERNAME));
		insert.putStringFieldDenyNull(UserStruc.PASSWORD, filter.getValue(UserStruc.PASSWORD));
		//日期类型输入以后加上
//		update.putStringFieldIgnoreNull(UserStruc.BIRTHEDAY, filter.getValue(UserStruc.BIRTHEDAY));
		insert.putStringFieldIgnoreNull(UserStruc.LANG, filter.getValue(UserStruc.LANG));
		insert.putStringFieldIgnoreNull(UserStruc.SEX, filter.getValue(UserStruc.SEX));
		insert.putStringFieldIgnoreNull(UserStruc.USERCODE, filter.getValue(UserStruc.USERCODE));
		insert.putStringFieldIgnoreNull(UserStruc.USER_MACHINE, filter.getValue(UserStruc.USER_MACHINE));
		//更新用户
		sql.add(insert.getSimpleSql());
	}
	
	/**
	 *  删除用户信息<BR>
	 *  参数:UserStruc.USERID
	 */
	private void delUser(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		StringBuilder sp = new StringBuilder();
		Delete delete = new Delete(this.tableName);
		sp.append(UserStruc.USERID);
		sp.append("='");
		sp.append(filter.getValue(UserStruc.USERID));
		sp.append("'");
		delete.setWhere(sp.toString());
		sql.add(delete.getSimpleSql());
	}
}
